//
//  RootViewController.m
//  BanBan2.0
//
//  Created by 副团长 on 16/6/15.
//  Copyright © 2016年 副团长. All rights reserved.
//

#import "RootViewController.h"
#import "BXTabBarController.h"
#import "UserManager.h"

#import "LoginMainViewController.h"

#define UserLogin @""BanBanBaoAPI@"/ofclogin/login"

@interface RootViewController ()
@property (nonatomic, strong)UIImageView *imagetemp;
@end

@implementation RootViewController





- (void)viewDidLoad {
    [super viewDidLoad];

    
    //  如果是已经登录的要重新  更新一下token
    if (_appdelegate != nil && [_appdelegate isEqualToString:@"AppDelegate"]) {
        _imagetemp= [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"引导页" ofType:@"png"];
        _imagetemp.image = [UIImage imageWithContentsOfFile:path];
        [self.view addSubview:_imagetemp];
     
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSString *account = [userDefaults objectForKey:@"account"];
        NSString *password = [userDefaults objectForKey:@"password"];
        [self loginAccount:account password:password];
        
    }else{
        
      [self setHomeVC];
        
    }
    
    

    
}


-(void)setHomeVC{

    self.backgroundImage = [UIImage imageNamed:@"Stars"];
    self.menuPreferredStatusBarStyle = 1; // UIStatusBarStyleLightContent
    self.scaleContentView = NO;     // 禁止缩小
    self.parallaxEnabled = NO;      // 禁止左右晃动（陀螺仪感觉）
    
    //    @property (assign, readwrite, nonatomic) BOOL panGestureEnabled;
    //    @property (assign, readwrite, nonatomic) BOOL panFromEdge;
    self.panGestureEnabled = YES;
    self.panFromEdge = YES;
    self.contentViewInPortraitOffsetCenterX = SCREEN_WIDTH/3;  //  左右视图屏占比例
    self.contentViewShadowColor = [UIColor blackColor];
    self.contentViewShadowOffset = CGSizeMake(0, 0);
    self.contentViewShadowOpacity = 0.6;
    self.contentViewShadowRadius = 12;
    self.contentViewShadowEnabled = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disableRESideMenu) name:@"disableRESideMenu" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enableRESideMenu) name:@"enableRESideMenu" object:nil];
}



-(void)loginAccount:(NSString*)account password:(NSString*)password{
    
    [SVProgressHUD showWithStatus:@"正在登录"];
    NSDictionary *jsonDic =  @{
                               @"commonParam":@{
                                       @"appKey":@"bbl40c2n1uq5zcvcvz",
                                       @"appVersion":AppVersion,
                                       @"page":@0,
                                       @"pagesize":@16
                                       },
                               @"object":@{
                                       @"channelId":DeviceToken,@"channelType":@"1",
                                       @"code":password,
                                       @"phone":account,
                                       @"state":@"1"
                                       }
                               };
    
    NSString *sign = [ChangeParameterWithMD5 getMd5WithCommonParamDic:jsonDic[@"commonParam"] Object:jsonDic[@"object"]];
    
    NSDictionary *json = @{
                           @"commonParam":@{
                                   @"appKey":@"bbl40c2n1uq5zcvcvz",
                                   @"appVersion":AppVersion,
                                   @"page":@0,
                                   @"pagesize":@16,
                                   @"sign":sign
                                   },
                           @"object":@{
                                   @"channelId":DeviceToken,@"channelType":@"1",@"code":password,@"phone":account,@"state":@"1"
                                   }
                           };
    
    [BanNetRequest BanNetRequestURL:UserLogin JsonParameters:json  timeoutInterval:2.0f  finish:^(NSDictionary *dic) {
        BXLog(@"登录%@",dic);
        [SVProgressHUD dismiss];
        if ([dic[@"status"] isEqualToString:@"0000"]){
            
            UserManager *manager = [UserManager shareInstance];
            /**
             * 此时 必然有个人信息 和 公司信息 token 状态会员或管理员
             */
            if (dic[@"data"][@"companyUserInfo"][@"userInfoOut"]!=nil) {
                [manager.userInfo setValuesForKeysWithDictionary:dic[@"data"][@"companyUserInfo"][@"userInfoOut"]];
            }
            
            if (dic[@"data"][@"companyUserInfo"][@"companyInfoOut"]!=nil) {
                [manager.companyInfo setValuesForKeysWithDictionary:dic[@"data"][@"companyUserInfo"][@"companyInfoOut"]];
            }
            manager.token = dic[@"data"][@"token"];
            [_imagetemp removeFromSuperview];
            [self setHomeVC];
            _appdelegate = @"";
            
        }else{
            [_imagetemp removeFromSuperview];
            _appdelegate = @"";
            LoginMainViewController *loginVC = [[LoginMainViewController alloc]init];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:loginVC];
            [self presentViewController:nav animated:NO completion:nil];
        }
        
    } error:^(NSError *error) {
        [SVProgressHUD dismiss];
        [_imagetemp removeFromSuperview];
        _appdelegate = @"";
        LoginMainViewController *loginVC = [[LoginMainViewController alloc]init];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:loginVC];
        [self presentViewController:nav animated:NO completion:nil];
    
    }];
    
}


- (void)enableRESideMenu {
    self.panGestureEnabled = YES;
}

- (void)disableRESideMenu {
    self.panGestureEnabled = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
